package com.bootdo.system.dao;

import com.bootdo.system.domain.Customer;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface CustomerMapper {

	@Select("select * from tb_customer where id = #{id}")
	Customer get(Long id);
	
	@Select("<script>" +
	"select * from tb_customer " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"linkerName != null and linkerName != ''\">"+ "and linkerName = #{linkerName} " + "</if>" + 
	  		  "<if test=\"CustomerType != null and CustomerType != ''\">"+ "and CustomerType = #{CustomerType} " + "</if>" + 
	  		  "<if test=\"ownerId != null and ownerId != ''\">"+ "and ownerId = #{ownerId} " + "</if>" + 
	  		  "<if test=\"bdId != null and bdId != ''\">"+ "and bdId = #{bdId} " + "</if>" + 
	  		 "<if test=\"status != null and status != ''\">"+ "and status = #{status} " + "</if>" + 
		  	"</where>"+ 
			" <choose>" + 
	            "<when test=\"sort != null and sort.trim() != ''\">" + 
	                "order by ${sort} ${order}" + 
	            "</when>" + 
				"<otherwise>" + 
	                "order by id desc" + 
				"</otherwise>" + 
	        "</choose>"+
			"</script>")
	List<Customer> list(Map<String,Object> map);
	
	@Select("<script>" +
	"select count(*) from tb_customer " + 
			"<where>" + 
			 "<if test=\"id != null and id != ''\">"+ "and id = #{id} " + "</if>" + 
	  		  "<if test=\"linkerName != null and linkerName != ''\">"+ "and linkerName = #{linkerName} " + "</if>" + 
	  		  "<if test=\"CustomerType != null and CustomerType != ''\">"+ "and CustomerType = #{CustomerType} " + "</if>" + 
	  		  "<if test=\"ownerId != null and ownerId != ''\">"+ "and ownerId = #{ownerId} " + "</if>" +  
	  		 "<if test=\"status != null and status != ''\">"+ "and status = #{status} " + "</if>" + 
		  	"</where>"+ 
			"</script>")
	int count(Map<String,Object> map);
	
	@Insert("insert into tb_customer (`customerName`, `licenceImgs`, `customerBrief`, `courseBrief`, `teacherBrief`,"
			+ " `status`, `customerAddress`, `linkerName`, `linkerPhone`, `takePercent`, `dropTime`, `bdId`, `bdName`,"
			+ " `logoImgs`, `runStatus`, `areaId`, `bizId`,`longitude`, `latitude`,  "
			+ " `createUserId`, `createTime`, `password`, `shopStartTime`, `shopEndTime`"
			+ ", `customerTypeId`, `customerTypeName`, `customerPhones`, `accordingImgs`, `orgCodeImg`, `taxRegisterImg`"
			+ ", `licenseCode`, `representative`, `registerAddress`, `licenseExpireTime`, `currentStep`,`updateTime`"
			+ ", `accountNo`, `bankDeposit`,`accountName`, "
			+ "`accountHolder`,`holderID`, `IDFrontImg`, `IDBackImg`, `holdImg`, `orgName`)"
	+ "values (#{customerName}, #{licenceImgs}, #{customerBrief}, #{courseBrief}, #{teacherBrief}, #{status},"
	+ "#{customerAddress}, #{linkerName}, #{linkerPhone}, #{takePercent}, #{dropTime}, #{bdId}, "
	+ "#{bdName},#{logoImgs},#{runStatus},#{areaId},#{bizId},#{longitude},#{latitude},#{createUserId}, now()"
	+ ", #{password}, #{shopStartTime}, #{shopEndTime}, #{customerTypeId}, #{customerTypeName}, #{customerPhones}, "
	+ "#{accordingImgs}, #{orgCodeImg}, #{taxRegisterImg}, #{licenseCode},"
	+ " #{representative}, #{registerAddress}, #{licenseExpireTime}, #{currentStep},now(), #{accountNo}, "
	+ "#{bankDeposit},#{accountName}, #{accountHolder},#{holderID},#{IDFrontImg},#{IDBackImg},#{holdImg},#{orgName})")
	@Options(useGeneratedKeys=true)
	int save(Customer bComments);
	
	@Update("<script>"+ 
			"update tb_customer " + 
					"<set>" + 
		            "<if test=\"id != null\">`id` = #{id}, </if>" + 
                    "<if test=\"customerName != null\">`customerName` = #{customerName}, </if>" + 
                    "<if test=\"licenceImgs != null\">`licenceImgs` = #{licenceImgs}, </if>" + 
                    "<if test=\"customerBrief != null\">`customerBrief` = #{customerBrief}, </if>" + 
                    "<if test=\"courseBrief != null\">`courseBrief` = #{courseBrief}, </if>" + 
                    "<if test=\"teacherBrief != null\">`teacherBrief` = #{teacherBrief}, </if>" +    
                    "<if test=\"status != null\">`status` = #{status}, </if>" +  
                    "<if test=\"customerAddress != null\">`customerAddress` = #{customerAddress}, </if>" +
                    "<if test=\"linkerName != null\">`linkerName` = #{linkerName}, </if>" +
                    "<if test=\"linkerPhone != null\">`linkerPhone` = #{linkerPhone}, </if>" +
                    "<if test=\"takePercent != null\">`takePercent` = #{takePercent}, </if>" +
                    "<if test=\"dropTime != null\">`dropTime` = #{dropTime}, </if>" +
                    "<if test=\"bdId != null\">`bdId` = #{bdId}, </if>" +
                    "<if test=\"bdName != null\">`bdName` = #{bdName}, </if>" +
                    "<if test=\"logoImgs != null\">`logoImgs` = #{logoImgs}, </if>" +
                    "<if test=\"runStatus != null\">`runStatus` = #{runStatus}, </if>" +
                    "<if test=\"areaId != null\">`areaId` = #{areaId}, </if>" +
                    "<if test=\"bizId != null\">`bizId` = #{bizId}, </if>" +
                    "<if test=\"longitude != null\">`longitude` = #{longitude}, </if>" +
                    "<if test=\"latitude != null\">`latitude` = #{latitude}, </if>" +
                    
				  "<if test=\"password != null\">`password` = #{password}, </if>" +
				  "<if test=\"shopStartTime != null\">`shopStartTime` = #{shopStartTime}, </if>" +
				  "<if test=\"shopEndTime != null\">`shopEndTime` = #{shopEndTime}, </if>" +
				  "<if test=\"customerTypeId != null and customerTypeId != 0\">`customerTypeId` = #{customerTypeId}, </if>" +
				  "<if test=\"customerTypeName != null\">`customerTypeName` = #{customerTypeName}, </if>" +
				  "<if test=\"customerPhones != null\">`customerPhones` = #{customerPhones}, </if>" +
				  "<if test=\"accordingImgs != null\">`accordingImgs` = #{accordingImgs}, </if>" +
				  "<if test=\"orgCodeImg != null\">`orgCodeImg` = #{orgCodeImg}, </if>" +
				  "<if test=\"taxRegisterImg != null\">`taxRegisterImg` = #{taxRegisterImg}, </if>" +
				  "<if test=\"licenseCode != null\">`licenseCode` = #{licenseCode}, </if>" +
				  "<if test=\"representative != null\">`representative` = #{representative}, </if>" +
				  "<if test=\"registerAddress != null\">`registerAddress` = #{registerAddress}, </if>" +
				  "<if test=\"licenseExpireTime != null\">`licenseExpireTime` = #{licenseExpireTime}, </if>" +
				  "<if test=\"currentStep != null\">`currentStep` = #{currentStep}, </if>" +
				  
				 "<if test=\"accountNo != null\">`accountNo` = #{accountNo}, </if>" + 
				 "<if test=\"bankDeposit != null\">`bankDeposit` = #{bankDeposit}, </if>" + 
				 "<if test=\"accountName != null\">`accountName` = #{accountName}, </if>" +    
				 "<if test=\"accountHolder != null\">`accountHolder` = #{accountHolder}, </if>" +    
				 "<if test=\"holderID != null\">`holderID` = #{holderID}, </if>" +    
				 "<if test=\"IDFrontImg != null\">`IDFrontImg` = #{IDFrontImg}, </if>" +  
				 "<if test=\"IDBackImg != null\">`IDBackImg` = #{IDBackImg}, </if>" +  
				 "<if test=\"holdImg != null\">`holdImg` = #{holdImg}, </if>" +
				 "<if test=\"orgName != null\">`orgName` = #{orgName}, </if>" + 
 
				  "`updateTime` = now(), " +
          			"</set>" + 
					"where id = #{id}"+
			"</script>")
	int update(Customer bComments);
	
	@Delete("delete from tb_customer where id =#{id}")
	int remove(Long id);
	
	@Delete("<script>"+ 
			"delete from tb_customer where id in " + 
					"<foreach item=\"id\" collection=\"array\" open=\"(\" separator=\",\" close=\")\">" + 
						"#{id}" + 
					"</foreach>"+
			"</script>")
	int batchRemove(Integer[] ids);
	
	
	@Select("select count(*) from tb_customer where id != #{id} and linkerPhone = #{linkerPhone}")
	int checkLinkerPhone(Map<String,Object> param);
	
	
	@Select("select * from tb_customer where linkerPhone = #{linkerPhone} limit 1")
	Customer getByPhone(String linkerPhone);
}
